package utils

import (
	"encoding/json"
	"io/ioutil"
	"log"
	"rbac-abac-demo/models"
)

var Users []models.User
var RBACPolicies []RBACPolicy
var ABACPolicies []ABACPolicy

func LoadConfig() {
	// 加载用户
	userBytes, err := ioutil.ReadFile("config/users.json")
	if err != nil {
		log.Fatal("无法加载 users.json:", err)
	}
	json.Unmarshal(userBytes, &Users)

	// 加载策略
	policyBytes, err := ioutil.ReadFile("config/policies.json")
	if err != nil {
		log.Fatal("无法加载 policies.json:", err)
	}

	var policies struct {
		RBAC []RBACPolicy `json:"rbac"`
		ABAC []ABACPolicy `json:"abac"`
	}
	json.Unmarshal(policyBytes, &policies)

	RBACPolicies = policies.RBAC
	ABACPolicies = policies.ABAC
}

// RBAC 策略
type RBACPolicy struct {
	Resource     string `json:"resource"`
	Action       string `json:"action"`
	RequiredRole string `json:"required_role"`
}

// ABAC 策略（支持嵌套条件）
type ABACPolicy struct {
	Resource   string                 `json:"resource"`
	Action     string                 `json:"action"`
	Conditions map[string]interface{} `json:"conditions"`
}
